import __canvas__ from '../canvas/index.js';
class toolClass {
    constructor(){

    }

    deepCopy(res){
        if(
            !res                        || 
            typeof res === 'string'     || 
            typeof res === 'boolean'    || 
            typeof res === 'number'
        ){
            console.log('提示','深拷贝只对数组和对象数据使用');
            return;
        }

        return fn(res);

        function fn(target){
            let result;
            if(typeof target === 'object'){
                if(Array.isArray(target)){
                    result = [];
                    for (let i in target) {
                        // 递归克隆数组中的每一项
                        result.push(fn(target[i]))
                    }
                }
                else if(target === null){
                    result = null;
                }
                else{
                    result = {};
                    for (let i in target) {
                        result[i] = fn(target[i]);
                    }
                }
            }
            else{
                result = target;
            }
            
            return result;
        }
    }

    MapTurnArr(res){
        let arr = [];
        for(let el of res){
            arr.push(el[1])            
        }

        return arr;
    }

    // 通过ID 获取宽高xy 和坐标点
    idTrunPoi(id){ 
        
        let obj = {
            w:0,
            h:0,
            x:0,
            y:0,
            point:{}
        };

        let el = document.getElementById(id);
        let rect = el.getBoundingClientRect();
        let w = rect.width;
        let h = rect.height;
        w = w/__canvas__.scale;
        h = h/__canvas__.scale;
        w = parseInt(w * 1000000)/1000000;
        h = parseInt(h * 1000000)/1000000;

        let x = parseFloat(el.style.left);
        let y = parseFloat(el.style.top);

        let a = [x , y];
        let b = [x + w , y];
        let c = [x + w , y + h];
        let d = [x , y + h];
        let ab = [x + w/2 , y];
        let ad = [x , y + h/2];
        let bc = [x + w , y + h/2];
        let cd = [x + w/2 , y + h];
        let mid = [x + w/2 , y + h/2];

        obj.w = w;
        obj.h = h;
        obj.x = x;
        obj.y = y;
        obj.point = {a,b,c,d,ab,bc,cd,ad,mid};
        
        return obj;
    }

    autoPoi(res){
        let w = res.w,
            h = res.h,
            x = res.x,
            y = res.y;

        let obj = {
            w,
            h,
            x,
            y,
            point:{}
        };

        let a = [x , y];
        let b = [x + w , y];
        let c = [x + w , y + h];
        let d = [x , y + h];
        let ab = [x + w/2 , y];
        let ad = [x , y + h/2];
        let bc = [x + w , y + h/2];
        let cd = [x + w/2 , y + h];
        let mid = [x + w/2 , y + h/2];

        obj.w = w;
        obj.h = h;
        obj.x = x;
        obj.y = y;
        obj.point = {a,b,c,d,ab,bc,cd,ad,mid};

        return obj
    }

    // 深度合并对象
    extends(a, b) { 
        for (var k in b) {
            if(a.hasOwnProperty(k)){
                if(typeof b[k] === "object"){
                    if(b[k] instanceof Array){
                        a[k] = b[k];
                    }
                    else if(b[k] instanceof Function){
                        a[k] = b[k];
                    }
                    else if(Object.keys(b[k] > 0)){
                        this.extends(a[k],b[k]);
                    }
                    
                }
                else{
                    a[k] = b[k];
                }
                
            }

        }
        return a;
    }
}

let tool = new toolClass();
export default tool;